﻿///// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

///*
//* settings for asynchronous content loading
//*/

var WasAsync = false;

// these actions cannot use async load
var NotAsyncLinks = ["/ListToExcel", "/Refresh"];

function IsAsyncLink(url) {
    for (var i = 0; i < window.NotAsyncLinks.length; i++) {
        if ($.trim(url).match(window.NotAsyncLinks[i]) == window.NotAsyncLinks[i]) {
            return false;
        }
    }
    return true;
}

$(document).ready(function () {
    var selector = " #masterBodySuperDummy"
    //        + ", #masterBody #listView CAPTION A[href]"
    //        + ", #masterBody #listView THEAD A[href]"
    //        + ", #masterBody #listView TBODY A[href]"
    //        + ", #masterRibb A[href]"
    //        + ", #masterPagi A[href]"
    //        + ", #asideMenuTVScroll A[href]"
    ;

    $(document).on("click", selector, function (e) {
        var url = $(this).attr('href');
        if (!IsAsyncLink(url)) {
            return true;
        }
        $.address.value(url);
        return false;
    });
    $.address.change(HandleAsyncListLoad);
});

function HandleAsyncListLoad(evnt) {
    var url = evnt.value;
    if (url == null || url == "/") {
        if (window.WasAsync) {
            window.location = window.location;
        }
        return;
    }
    window.WasAsync = true;
    var source = url + (url.indexOf('?') > 0 ? "&" : "?") + "MP=asc&rand=" + new Date().getTime();
    source = source.replace("/http://", "http://");

    $.get(source, function (data) {
        $("#masterBody").html($($(data).find("#masterBody")).html());
        $("#masterRibb .menu").html($($(data).find("#masterRibb .menu")).html());
        $("#masterRibb .icons").html($($(data).find("#masterRibb .icons")).html());
        $("#masterRibb .texts").html($($(data).find("#masterRibb .texts")).html());
        $("#masterLang").html($($(data).find("#masterLang")).html());
        $("#masterPagi").html($($(data).find("#masterPagi")).html());
        $("#masterLout").html($($(data).find("#masterLout")).html());
        $("#masterActions").html($($(data).find("#masterActions")).html());
        $("#masterButtons").html($($(data).find("#masterButtons")).html());
        try {
            $("HEAD TITLE").html($($(data).find("#masterTitle")).html());
        } catch (ex) { }
        $("#masterBody").attr("class", $($(data).find("#masterBody")).attr("class"));
        UpdateRulesScriptPath($($(data).find("#masterHead INPUT")));
        HandleAllAsync();
    });
}

function UpdateRulesScriptPath(data) {
    if (data == undefined || data.length == 0) {
        return;
    }

    var path = $(data).data("rulespath");
    if (path == undefined || path == "") {
        return;
    }

    window.validator = { rules: [], messages: [] }; // reset/clear current rules

    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", path);
    script.onload = function () {
        $("#" + window._mainForm).validate(window.validator);
    };
    document.getElementsByTagName("head")[0].appendChild(script);
}